/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ru.vsu.cs.piit.vokod.controller;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import net.sf.jasperreports.engine.JasperPrint;
import ru.vsu.cs.piit.hibernate.GenericDAO.GenericHibernateDAO;
import ru.vsu.cs.piit.vokod.DAO.DAOFactory;
import ru.vsu.cs.piit.vokod.IListListener;
import ru.vsu.cs.piit.vokod.ObservableArrayList;
import ru.vsu.cs.piit.vokod.model.DailyMove;
import ru.vsu.cs.piit.vokod.model.Diagnos;
import ru.vsu.cs.piit.vokod.model.GroupOfDiagnosis;
import ru.vsu.cs.piit.vokod.model.GroupOfTreatment;
import ru.vsu.cs.piit.vokod.model.Pacient;
import ru.vsu.cs.piit.vokod.model.Treatment;
import ru.vsu.cs.piit.vokod.model.Ward;
import ru.vsu.cs.piit.vokod.reports.ReportFactory;
import ru.vsu.cs.piit.vokod.view.MainFrame;
import ru.vsu.cs.piit.vokod.view.MyDialog;
import ru.vsu.cs.piit.vokod.view.panels.DailyMovePanel;
import ru.vsu.cs.piit.vokod.view.panels.DiagnosPanel;
import ru.vsu.cs.piit.vokod.view.panels.GroupOfDiagnosPanel;
import ru.vsu.cs.piit.vokod.view.panels.GroupOfTreatmentPanel;
import ru.vsu.cs.piit.vokod.view.panels.PacientPanel;
import ru.vsu.cs.piit.vokod.view.panels.TreatmentPanel;
import ru.vsu.cs.piit.vokod.view.panels.WardPanel;

/**
 *
 * @author Галчонок
 */
public class Controller {

    ObservableArrayList<DailyMove> dailyMoveList;
    ObservableArrayList<Pacient> pacientsList;

    MainFrame mainFrame;
    List<MyDialog> dialogs = new ArrayList();

    PacientPanel pacientPanel;
    DailyMovePanel dailyMovePanel;
    WardPanel wardPanel;
    GroupOfDiagnosPanel groupOfDiagnosPanel;
    DiagnosPanel diagnosPanel;
    TreatmentPanel treatmentPanel;
    GroupOfTreatmentPanel groupOfTreatmentPanel;

    static Controller ctrl;

    private Controller() {
        dailyMoveList = new ObservableArrayList(GenericHibernateDAO.findAll(DailyMove.class));

        dailyMoveList.AddListener(new IListListener<DailyMove>() {

            @Override
            public void doSave(DailyMove object) {
                GenericHibernateDAO.save(object);
            }

            @Override
            public void doRemove(DailyMove object) {
                GenericHibernateDAO.delete(object);
            }

        });

        pacientsList = new ObservableArrayList<Pacient>(DAOFactory.getPacientDAO().findAll());
        pacientsList.AddListener(new IListListener<Pacient>() {

            @Override
            public void doSave(Pacient object) {
                GenericHibernateDAO.save(object);
            }

            @Override
            public void doRemove(Pacient object) {
                GenericHibernateDAO.delete(object);
            }

        });

        mainFrame = new MainFrame(dailyMoveList, pacientsList);
        mainFrame.setVisible(true);
    }

    public void addDailyMove()
    {
        editDailyMove(null);
    }

    public void editDailyMove(DailyMove dailyMove)
    {
        MyDialog dlg;
        if (dailyMove!=null)
            dailyMovePanel = new DailyMovePanel(dailyMove);
        else
            dailyMovePanel = new DailyMovePanel();
        dlg = new MyDialog(dailyMovePanel);
        dialogs.add(dlg);
        dlg.setTitle("Редактирование записи о движении в стационаре");
        dlg.setModal(true);
        dlg.addCancelActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                dialogs.get(dialogs.size()-1).setVisible(false);
                dialogs.remove(dialogs.size()-1);
            }
        });
        dlg.addOkActionListener(new ActionListener() {

           @Override
             public void actionPerformed(ActionEvent e) {
                dialogs.get(dialogs.size()-1).setVisible(false);
                dialogs.remove(dialogs.size()-1);
                DailyMove dailyMove = dailyMovePanel.getDailyMove();
                if (dailyMove.getMoveId()==null)
                    dailyMoveList.add(dailyMove);
                else dailyMoveList.IsChanged(dailyMove);
            }
        });
        dlg.setVisible(true);
    }

    public void addPacient()
    {
        editPacient(null);
        //dialogs.get(dialogs.size()-1).setTitle("Новый пациент");
    }

    public void editPacient(Pacient p)
    {
        MyDialog dlg;
        if (p!=null)
            pacientPanel = new PacientPanel(p);
        else
            pacientPanel = new PacientPanel();
        dlg = new MyDialog(pacientPanel);
        dialogs.add(dlg);
        dlg.setTitle("Редактирование записи о пациенте");
        dlg.setModal(true);
        dlg.addCancelActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                dialogs.get(dialogs.size()-1).setVisible(false);
                dialogs.remove(dialogs.size()-1);
            }
        });
        dlg.addOkActionListener(new ActionListener() {

           @Override
             public void actionPerformed(ActionEvent e) {
                dialogs.get(dialogs.size()-1).setVisible(false);
                dialogs.remove(dialogs.size()-1);
                Pacient p = pacientPanel.getPacient();
                if (p.getPacientId()==null)
                    pacientsList.add(p);
                else pacientsList.IsChanged(p);
            }
        });
        dlg.setVisible(true);
    }

    public void addWard()
    {
        editWard(null);
        //dialogs.get(dialogs.size()-1).setTitle("Новое отделение");
    }

    public void editWard(Ward w)
    {
        MyDialog dlg;
        if (w!=null)
            wardPanel = new WardPanel(w);
        else
            wardPanel = new WardPanel();
        dlg = new MyDialog(wardPanel);
        dialogs.add(dlg);
        dlg.setTitle("Редактирование записи об отделении");
        dlg.setModal(true);
        dlg.addCancelActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                dialogs.get(dialogs.size()-1).setVisible(false);
                dialogs.remove(dialogs.size()-1);
            }
        });
        dlg.addOkActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                dialogs.get(dialogs.size()-1).setVisible(false);
                dialogs.remove(dialogs.size()-1);
                Ward w = wardPanel.getWard();
                if (w.getWardId()==null)
                    Maps.getWardList().add(w);
                else Maps.getWardList().IsChanged(w);
            }
        });
        dlg.setVisible(true);
    }

    public void addGroupOfDiagnoses()
    {
        editGroupOfDiagnoses(null);
        //dialogs.get(dialogs.size()-1).setTitle("Новая группа диагнозов");
    }

    public void editGroupOfDiagnoses(GroupOfDiagnosis g)
    {
        MyDialog dlg;
        if (g!=null)
            groupOfDiagnosPanel = new GroupOfDiagnosPanel(g);
        else
            groupOfDiagnosPanel = new GroupOfDiagnosPanel();
        dlg = new MyDialog(groupOfDiagnosPanel);
        dialogs.add(dlg);
        dlg.setTitle("Редактирование записи о группе диагнозов");
        dlg.setModal(true);
        dlg.addCancelActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                dialogs.get(dialogs.size()-1).setVisible(false);
                dialogs.remove(dialogs.size()-1);
            }
        });
        dlg.addOkActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                dialogs.get(dialogs.size()-1).setVisible(false);
                dialogs.remove(dialogs.size()-1);
                GroupOfDiagnosis g = groupOfDiagnosPanel.getGroupOfDiagnosis();
                if (g.getGroupOfDiagnosisId()==null)
                    Maps.getGroupOfDiagnosisesList().add(g);
                else Maps.getGroupOfDiagnosisesList().IsChanged(g);
            }
        });
        dlg.setVisible(true);
    }

    public void addDiagnos()
    {
        editDiagnos(null);
        //dialogs.get(dialogs.size()-1).setTitle("Новая группа диагнозов");
    }

    public void editDiagnos(Diagnos d)
    {
        MyDialog dlg;
        if (d!=null)
            diagnosPanel = new DiagnosPanel(d);
        else
            diagnosPanel = new DiagnosPanel();
        dlg = new MyDialog(diagnosPanel);
        dialogs.add(dlg);
        dlg.setTitle("Редактирование записи о группе диагнозов");
        dlg.setModal(true);
        dlg.addCancelActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                dialogs.get(dialogs.size()-1).setVisible(false);
                dialogs.remove(dialogs.size()-1);
            }
        });
        dlg.addOkActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                dialogs.get(dialogs.size()-1).setVisible(false);
                dialogs.remove(dialogs.size()-1);
                Diagnos d = diagnosPanel.getDiagnos();
                if (d.getDiagnosId()==null)
                    Maps.getDiagnosList().add(d);
                else Maps.getDiagnosList().IsChanged(d);
            }
        });
        dlg.setVisible(true);
    }

    public void addTreatment()
    {
        editTreatment(null);
    }

    public void editTreatment(Treatment t)
    {
        MyDialog dlg;
        if (t!=null)
            treatmentPanel = new TreatmentPanel(t);
        else
            treatmentPanel = new TreatmentPanel();
        dlg = new MyDialog(treatmentPanel);
        dialogs.add(dlg);
        dlg.setTitle("Редактирование записи о виде лечения");
        dlg.setModal(true);
        dlg.addCancelActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                dialogs.get(dialogs.size()-1).setVisible(false);
                dialogs.remove(dialogs.size()-1);
            }
        });
        dlg.addOkActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                dialogs.get(dialogs.size()-1).setVisible(false);
                dialogs.remove(dialogs.size()-1);
                Treatment t = treatmentPanel.getTreatment();
                if (t.getTreatmentId()==null)
                    Maps.getTreatmentList().add(t);
                else Maps.getTreatmentList().IsChanged(t);
            }
        });
        dlg.setVisible(true);
    }

    public void addGroupOfTreatment()
    {
        editGroupOfTreatment(null);
    }

    public void editGroupOfTreatment(GroupOfTreatment g)
    {
        MyDialog dlg;
        if (g!=null)
            groupOfTreatmentPanel = new GroupOfTreatmentPanel(g);
        else
            groupOfTreatmentPanel = new GroupOfTreatmentPanel();
        dlg = new MyDialog(groupOfTreatmentPanel);
        dialogs.add(dlg);
        dlg.setTitle("Редактирование записи о группе видов лечения");
        dlg.setModal(true);
        dlg.addCancelActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                dialogs.get(dialogs.size()-1).setVisible(false);
                dialogs.remove(dialogs.size()-1);
            }
        });
        dlg.addOkActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                dialogs.get(dialogs.size()-1).setVisible(false);
                dialogs.remove(dialogs.size()-1);
                GroupOfTreatment t = groupOfTreatmentPanel.getGroupOfTreatment();
                if (t.getGroupOfTreatmentId()==null)
                    Maps.getGroupOfTreatmentsList().add(t);
                else Maps.getGroupOfTreatmentsList().IsChanged(t);
            }
        });
        dlg.setVisible(true);
    }

    public JasperPrint getReport(String name, Map params)
    {
        JasperPrint jrprint = ReportFactory.getReport(name, params);
        return jrprint;
    }
    
    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        Controller.getInstance();
    }
    
    public static Controller getInstance(){
        if (ctrl == null) 
            ctrl = new Controller();
        return ctrl;
    }
}
